﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;
using ClassLibrary.Entity;

namespace ClassLibrary.DAO
{
    public class TblBookingDAO
    {
        private DBService _db = null;
        public TblBookingDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }

        public bool Insert(TblBooking booking, List<TblBookingDetail> lstDetail, out int BookID)
        {
            BookID = 0;
            bool flag = false;
            try
            {

                DataTable dt = _db.ExeSP_DataTable("sp_TblBookingIns",
                   new string[] { "@DateBook", "@DateActive", "@Status", "@Node", "@CusID", "@TotalPrice", "@PaymentMethod" },
                   new object[] { 
                                       Convert.ToDateTime(booking.DateBook),
                                       Convert.ToDateTime(booking.DateActive),
                                        Code.FixIntNull(booking.Status).ToString(),
                                        Code.FixStringNull(booking.Notes),
                                        Code.FixIntNull(booking.CusID).ToString(),
                                        Convert.ToDecimal(booking.TotalPrice),
                                        Code.FixIntNull(booking.PaymentMethod),
                 
                    }, CommandType.StoredProcedure);

                foreach (TblBookingDetail bookingDetail in lstDetail)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        bookingDetail.TotalAmount = bookingDetail.Quantity * bookingDetail.Price;

                        _db.ExeSP_DataTable("sp_TblBookingDetail_Ins",
                          new string[] { "@BookID", "@IDEmail", "@Quantity", "@Price", "@TotalAmount", "@Discount" },
                          new object[] { dr["BookID"],    
                                    Code.FixIntNull(bookingDetail.IDEmail).ToString(),
                                    Code.FixIntNull(bookingDetail.Quantity).ToString(),
                                    Convert.ToDecimal(bookingDetail.Price), 
                                    Convert.ToDecimal(bookingDetail.TotalAmount),
                                    bookingDetail.Discount},
                                         CommandType.StoredProcedure);

                        BookID = Convert.ToInt32(dr["BookID"].ToString());
                    }

                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

       
        public DataTable Update(int Status, int BookID)
        {
            DataTable dt = null;
            try
            {

                dt = _db.ExeSP_DataTable("Update TblBooking set Status= @Status where BookID = @BookID",
                    new string[] { "@Status", "@BookID" },
                    new object[] { Status, BookID }, CommandType.Text);


            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public bool UpdateDateActive(int BookID, DateTime DateActive)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("update TblBooking set DateActive = @DateActive where BookID = @BookID",
                    new string[] { "@BookID", "@DateActive" },
                    new object[] { BookID, DateActive },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable EmailBookingDetail(int BookID)
        {
            DataTable dt = null;
            try
            {

                dt = _db.ExeSP_DataTable("select * from TblBookingDetail where BookID = @BookID",
                    new string[] { "@BookID" },
                    new object[] { BookID }, CommandType.Text);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
